package com.example.demo.controller.impor;

import com.example.demo.dbo.User;
import com.example.demo.dbo.User222;
import com.example.demo.util.JsonUtil;
import lombok.RequiredArgsConstructor;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequiredArgsConstructor
public class UploadController {
    private final ImportService importService;

    @PostMapping("/getUU")
    public String getUU(@RequestBody User222 user) {
        System.out.println("user:"+user);
        System.out.println("xxx"+JsonUtil.toJson(user));
        return "";
    }

    @PostMapping("/upload")
    public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile file) {
        try {
            importService.importData(file.getInputStream());
            return ResponseEntity.ok("数据导入成功");
        } catch (IOException e) {
            return ResponseEntity.status(500).body("文件读取失败");
        } catch (RuntimeException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    public static void main(String[] args) {
        String ss = "http://10.101.221.232/qk/iv_1118";
        String replace2 = ss.replace("10.101.221.232", "172.29.0.51");
        System.out.println(replace2);
        String uuu = "{\"taskId\":1741078257139197,\"taskType\":\"split3\",\"notifyQueue\":\"notify-java\",\"pipelineId\":\"17359821061053231\",\"input\":\"http://vlive.peopleurl.cn/broadcast/activity/1740709221483186/record.m3u8\",\"outputs\":[{\"splitOutputId\":1741078257139197,\"splitParts\":[{}],\"objects\":[\"upload/vod/user1735982106105323/1741078257139197/4/video.mp4\"]}]}";
        String replace1 = uuu.replaceAll("vlive.peopleurl.cn", "888.888");
        System.out.println(replace1);
        String url = "http://localhost:8080/upload";
        String replace = url.replace("localhost:8080", "localhost:8888");
        System.out.println(replace);
        String chineseName = "wendao";
        String pinyin = convertToPinyin(chineseName);
        System.out.println("中文名: " + chineseName);
        System.out.println("拼音: " + pinyin);
    }

    public static String convertToPinyin(String chinese) {
        // 创建拼音输出格式对象
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        // 设置拼音大小写格式，这里设置为小写
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        // 设置拼音声调格式，这里不显示声调
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        // 设置 'ü' 的显示格式，这里用 'v' 表示
        format.setVCharType(HanyuPinyinVCharType.WITH_V);

        StringBuilder pinyin = new StringBuilder();
        for (char c : chinese.toCharArray()) {
            // 判断字符是否为中文字符
            if (Character.toString(c).matches("[\\u4E00-\\u9FA5]+")) {
                try {
                    // 获取该字符的拼音数组，可能有多个读音
                    String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c, format);
                    if (pinyinArray != null && pinyinArray.length > 0) {
                        // 取第一个读音作为结果
                        pinyin.append(pinyinArray[0]);
                    }
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            } else {
                // 非中文字符直接添加
                pinyin.append(c);
            }
        }
        return pinyin.toString();
    }
}
